package com.axonactive.yolo.util;

import java.util.Calendar;
import java.util.Date;

import com.axonactive.yolo.domain.SubscribedFlight;
import com.axonactive.yolo.dto.DateTimeDescription;

public class ConvertUnixTime {

	public DateTimeDescription getLocalTimeBaseOnJDK(String unixTimeFromAPIString) {
		
		long unixTimeFromAPI = Long.valueOf(unixTimeFromAPIString);
		Calendar thisOS = Calendar.getInstance();
		long thisOSTimeZoneRawOffset = thisOS.getTimeZone().getRawOffset() / 1000;
		String localDateTime = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date((unixTimeFromAPI - thisOSTimeZoneRawOffset) * 1000));
		DateTimeDescription dateTimeDescription = new DateTimeDescription();
		String monthString = localDateTime.substring(0, 2);
		String dateString = localDateTime.substring(3, 5);
		String yearString = localDateTime.substring(6, 10);
		String hourString = localDateTime.substring(11, 13);
		String minuteString = localDateTime.substring(14, 16);

		dateTimeDescription.setNAM(Integer.valueOf(yearString));
		dateTimeDescription.setTHANG(Integer.valueOf(monthString));
		dateTimeDescription.setNGAY(Integer.valueOf(dateString));
		dateTimeDescription.setGIO(Integer.valueOf(hourString));
		dateTimeDescription.setPHUT(Integer.valueOf(minuteString));

		return dateTimeDescription;
	}
	
	public static boolean isFlightUnvailable(SubscribedFlight sub) {
		long unixDepartureTime=Long.parseLong(sub.getUnixDepartureTime());
		Date date=new Date(unixDepartureTime*1000L);
		Date osDate=new Date();
		if (date.before(osDate)) {
			return true;
		}
		return false;
	}
}
